import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# 1. Take a random 1% sample (Crucial for performance)
pdf_subset = df.select("ra", "dec").sample(fraction=0.01, seed=42).toPandas()
# 2. Re-create the Region logic (using numpy to avoid errors)
pdf_subset['Region'] = pdf_subset['dec'].apply(lambda x: 'Galactic Plane' if np.abs(x) < 15 else 'Galactic Halo')
# 3. SET THE THEME: Dark Background for a "Space" look
plt.style.use('dark_background')
plt.figure(figsize=(12, 7))
# 4. Plot the "Halo" (Background Stars)
# We plot these first in cool blue so they look "distant"
sns.scatterplot(
data=pdf_subset[pdf_subset['Region'] == 'Galactic Halo'],
x='ra',
y='dec',
color='cornflowerblue',
s=5, # Small dots
alpha=0.3, # Faint transparency
edgecolor=None,
label='Galactic Halo (Sparse)'
)
# 5. Plot the "Plane" (The Disk)
# We plot these on top in bright Gold to represent the dense star field
sns.scatterplot(
data=pdf_subset[pdf_subset['Region'] == 'Galactic Plane'],
x='ra',
y='dec',
color='#FFD700', # Gold color
s=10, # Slightly larger dots
alpha=0.4, # Brighter
edgecolor=None,
label='Galactic Plane (Dense)'
)
# Draw the cut-off lines
plt.axhline(15, color='white', linestyle='--', linewidth=1, alpha=0.5)
plt.axhline(-15, color='white', linestyle='--', linewidth=1, alpha=0.5)
# Add text labels on the graph
plt.text(180, 0, "Milky Way Disk\n(High Density)", color='orangered',
ha='center', va='center', fontsize=12, )
plt.text(180, 60, "Galactic Halo\n(Low Density)", color='cornflowerblue',
ha='center', va='center', fontsize=10)
# 7. Final Polish
plt.title("Spatial Structure: The 'Flat' Disk vs. The 'Round' Halo", fontsize=14, color='white')
plt.xlabel("Right Ascension (Longitude)", fontsize=12)
plt.ylabel("Declination (Latitude)", fontsize=12)
plt.legend(loc='upper right', facecolor='black', edgecolor='white')
plt.grid(False) # Turn off grid to look more like space
# Astronomers view the sky looking "up", so we invert the X-axis
plt.gca().invert_xaxis()
plt.show()